package com.stylefeng.guns.rest.modular.viewrecord.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.stylefeng.guns.rest.common.persistence.dao.ViewRecordMapper;
import com.stylefeng.guns.rest.common.persistence.model.ViewRecord;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArticleDetailResponse;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleRequest;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.RecordListRequest;
import com.stylefeng.guns.rest.modular.viewrecord.controller.dto.ViewRecordResponse;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS-PC on 2018/6/28.
 */
@Service
public class ViewRecordService {
    @Autowired
    ViewRecordMapper viewRecordMapper;

    //查询是否有观看记录
    public boolean searchViewRecord(ArticleRequest request){
        List<ArticleDetailResponse> list = viewRecordMapper.selectList(Condition.create().eq("title_id" , request.getArticleId())
                .eq("user_id",request.getUserId()));
        if(list!=null && list.size()>0){
            return true;
        }
        return false;
    }

    //插入用户观看记录表
    public void insetViewRecord(int userId,int articleId){
        ViewRecord viewRecord = new ViewRecord();
        viewRecord.setArticleId(articleId);
        viewRecord.setUserId(userId);
        viewRecord.setCreate_time(new Date());
        viewRecordMapper.insert(viewRecord);
    }
    //删除观看记录
    public boolean deleteRecord(int recordId){
        return viewRecordMapper.delete(com.baomidou.mybatisplus.mapper.Condition.create().eq("id",recordId))
                >0?true:false;
    }

    //获取观看记录列表
    public List<ViewRecordResponse> queryViewRecords(RecordListRequest request){
        Map<String,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return viewRecordMapper.queryViewRecords(data);
    }
}
